package com.ztgf.shopping.service.epg;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.shopping.models.bo.epg.EpgShopBannerBO;
import com.ztgf.shopping.models.constant.ShoppingConstant;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;

import com.ztgf.shopping.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.mapper.epg.EpgShopBannerMapper;
import com.ztgf.shopping.models.service.epg.IEpgShopBannerService;
import com.ztgf.shopping.models.bo.epg.EpgShopBannerSaveBO;
import com.ztgf.shopping.models.dto.epg.EpgShopBannerDTO;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 店铺页banner控制器
 *
 * @author YUE
 * @Date 2020-05-28 18:38:51
 */
@DubboService
public class EpgShopBannerServiceImpl extends ServiceImpl<EpgShopBannerMapper, EpgShopBannerDTO> implements IEpgShopBannerService {

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    EpgShopBannerMapper epgShopBannerMapper;

    @Override
    public boolean add(EpgShopBannerSaveBO saveBO) {
        EpgShopBannerDTO dto = new EpgShopBannerDTO();
        BeanUtils.copyProperties(saveBO, dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    /**
     * 删除功能功能相关的店铺页banner
     *
     * @return
     */
    @Override
    public boolean deleteByModuleId(Long moduleId) {
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("module_id", moduleId);
        return epgShopBannerMapper.deleteByMap(columnMap) > 0;
    }

    /**
     * 根据店铺id查询商品详情的banner
     *
     * @param shopId
     * @return
     */
    @Override
    public List<EpgShopBannerDTO> getShopAdvertBannerListByShopId(Long shopId) {
        Wrapper<EpgShopBannerDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_id", shopId);
        wrapper.in("banner_type", Arrays.asList("3", "4"));//商品详情页得banner和底图
        return selectList(wrapper);
    }

    /**
     * 根据条件查询 店铺页banner 有页码
     *
     * @param epgShopBannerBO
     * @return
     */
    @Override
    public Page<EpgShopBannerDTO> getBySelectShopBannerInfoPage(EpgShopBannerBO epgShopBannerBO) {
        epgShopBannerBO = addMustData(epgShopBannerBO);
        Wrapper<EpgShopBannerDTO> wrapper = getBySelectShopBannerInfo(epgShopBannerBO);
        return selectPage(new Page<>(epgShopBannerBO.getPageNo(), epgShopBannerBO.getPageSize()), wrapper);
    }

    /**
     * 根据条件查询 店铺页banner 无页码
     *
     * @param epgShopBannerBO
     * @return
     */
    @Override
    public List<EpgShopBannerDTO> getBySelectShopBannerInfoNoPage(EpgShopBannerBO epgShopBannerBO) {
        epgShopBannerBO = addMustData(epgShopBannerBO);
        Wrapper<EpgShopBannerDTO> wrapper = getBySelectShopBannerInfo(epgShopBannerBO);
        if (epgShopBannerBO.getPageNo() == 0 && epgShopBannerBO.getPageSize() == 0)
            return epgShopBannerMapper.selectPage(new Page<>(0, ShoppingConstant.SHOP_BANNER_NUM), wrapper);
        return selectList(wrapper);
    }

    /**
     * 添加必要数据
     *
     * @param epgShopBannerBO
     * @return
     */
    private EpgShopBannerBO addMustData(EpgShopBannerBO epgShopBannerBO) {
        epgShopBannerBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
        epgShopBannerBO.setStatus(ShoppingCodeEnum.STATUS_0.getCode());
        epgShopBannerBO.setSort(ShoppingCodeEnum.SORT_ASC.getCode());
        return epgShopBannerBO;
    }

    private Wrapper<EpgShopBannerDTO> getBySelectShopBannerInfo(EpgShopBannerBO epgShopBannerBO) {
        Wrapper<EpgShopBannerDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("status", epgShopBannerBO.getStatus());
        wrapper.eq("is_publish", epgShopBannerBO.getIsPublish());
        wrapper.eq("module_id", epgShopBannerBO.getModuleId());
        if (ShoppingCodeEnum.SORT_DESC.getCode() == epgShopBannerBO.getSort())
            wrapper.orderBy("sort", false);
        else
            wrapper.orderBy("sort", true);
        return wrapper;
    }
}
